package com.snake.spring.boot.smaple.retrofit.controller;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.snake.spring.boot.smaple.retrofit.client.HuaNongBizClient;
import com.snake.spring.boot.smaple.retrofit.client.bo.*;
import com.snake.spring.boot.smaple.retrofit.client.dto.BusinessData;
import com.snake.spring.boot.smaple.retrofit.utils.Tools;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * @author cheng
 */
@Controller
@RequestMapping("/huanong")
public class HuaNongController {

    @Resource
    private HuaNongBizClient huaNongBizClient;

    @PostMapping(value = "/login")
    @ResponseBody
    public String login(@RequestHeader(value = "token", required = false) String token,
                        @RequestHeader(value = "transCode") String transCode, @RequestBody UserInfo userInfo) {
        BuildRequestHead buildRequestHead = new BuildRequestHead(token, transCode).invoke();
        GenericReq genericReq = buildRequestHead.getGenericReq();

        //requestbody sample  {"userCode":"41615044","comCode":"416100030005","agentCode":"U41002000043","agreementNo":"U41002000043-01","opertorCode":"41615044","saleSys":"53"}
        genericReq.setAttribute("head", buildRequestHead.getHeaderMap());

        genericReq.setAttribute("userCode", userInfo.getUserCode());
        genericReq.setAttribute("comCode", userInfo.getComCode());
        genericReq.setAttribute("agentCode", userInfo.getAgentCode());
        genericReq.setAttribute("agreementNo", userInfo.getAgreementNo());
        genericReq.setAttribute("opertorCode", userInfo.getOpertorCode());
        genericReq.setAttribute("SaleSys", userInfo.getSaleSys());
        String signString = Tools.encryptByPublicKey(JSONObject.toJSONString(genericReq));


//        String result = HttpRequest.post("https://apple-channel-cx.chinahuanong.com.cn/singlePlatform/thirdParty.do")
//                .header("Content-Type", "application/json")
//                .body(signString)
//                .execute().body();
        //return result;

        return huaNongBizClient.login(JSONObject.parseObject(signString));
    }


    @PostMapping(value = "/modelsQuery")
    @ResponseBody
    public ResponseEntity<String> modelsQuery(@RequestHeader(value = "token") String token,
                                              @RequestHeader(value = "transCode") String transCode, @RequestBody Map<String, String> requestMap) {
        BuildRequestHead buildRequestHead = new BuildRequestHead(token, transCode).invoke();
        GenericReq genericReq = buildRequestHead.getGenericReq();


        genericReq.setAttribute("head", buildRequestHead.getHeaderMap());
        genericReq.setAttribute("ModelName", "锋范HG7154CAA轿车");
        genericReq.setAttribute("ModelCode", "BGQESEUC0005");
        genericReq.setAttribute("reqType", "0");
        //genericReq.setAttribute("plateNo", "京AD1HND");
        String signString = Tools.encryptByPublicKey(JSONObject.toJSONString(genericReq));


        String result = HttpRequest.post("https://apple-channel-cx.chinahuanong.com.cn/singlePlatform/thirdParty.do")
                .header("Content-Type", "application/json")
                .body(signString)
                .execute().body();

        JSONArray jsonArrayModel = (JSONArray) JSONObject.parseObject(result).get("simpleStadarCar");
        JSONObject jsonObjectCarModel = (JSONObject) jsonArrayModel.get(0);
        return ResponseEntity.ok(result);

        //return huaNongBizClient.modelsQuery(JSONObject.parseObject(signString));
    }


    //{"orderNo":"8a80938d8ab6af16018ab6b406f31b80"}
    @PostMapping(value = "/quotedPrice")
    @ResponseBody
    public String quotedPrice(@RequestHeader(value = "token") String token,
                              @RequestHeader(value = "transCode") String transCode, @RequestBody Map<String, String> requestMap) throws Exception {
        BusinessData businessData = new BusinessData();
        businessData.setBusinessMain(new BusinessMain());
        //request head
        RequestHead requestHead = new RequestHead();
        requestHead.setInterfaceCode("ZTYQ");
        requestHead.setToken(token);
        requestHead.setTransCode(transCode);
        requestHead.setTransType("Req");
        businessData.setHead(requestHead);


        List<Contract> contract = new ArrayList<Contract>();

        Contract contractNew = new Contract();
        ContractMain contractMain = new ContractMain();
        contractMain.setValidDate("2023-10-29 00:00:00");
        contractMain.setExpiryDate("2024-07-28 23:59:59");
        contractMain.setRiskCode("0507");
        contractMain.setProposalType("1");


        List<Coverage> coverage = new ArrayList<>();
        Coverage coverageNew = new Coverage();
        coverageNew.setKindCode("BZ");
        coverageNew.setKindName("机动车交通事故责任强制保险");
        coverageNew.setQuantity(1);
        coverageNew.setRelatedInd("0");
        coverageNew.setServicetimes(1);

        coverage.add(coverageNew);

        contract.add(contractNew);
        Tax tax = new Tax();
        tax.setTaxConditionCode("1N");
        tax.setTaxPayerIdentificationCode("14020319951205702X");
        tax.setTaxPayerIdentity("01");
        tax.setTaxPayerIdentityNo("14020319951205702X");
        tax.setTaxPayerIdentityType("01");
        tax.setTaxPayerName("张三");
        contractNew.setContractMain(contractMain);
        contractNew.setCoverage(coverage);
        contractNew.setTax(tax);
        contractNew.setPlatFormMessageBean(new PlatFormMessageBean());
        businessData.setContract(contract);
        List<Customer> customer = new ArrayList<Customer>();
        String[] roles = {"1", "2", "3"};
        Arrays.stream(roles).forEach(v -> {
            Customer customerNew = new Customer();
            if ("1".equals(v)) {
                customerNew.setAddr("社区");
                customerNew.setAnnualSalary("M01");
                customerNew.setBirthday("1990-07-08 09:00:00");
                customerNew.setCervalidDate("2023-06-12 00:00:00");
                customerNew.setCity("130100");
                customerNew.setCreendDate("2043-06-12 00:00:00");
                customerNew.setDetailedAddressInfo("河北省石家庄市长安区社区");
                customerNew.setDistrict("130102");
                customerNew.setEmail("555@163.com");
                customerNew.setType("02");
                customerNew.setIdentifyValidDate("2023-06-12 02:46:24");
                customerNew.setIdentifyValidEndDate("2043-06-12 12:00:00");
                customerNew.setNation("1");
                customerNew.setJob("P0001");
                customerNew.setNationality("156");
                customerNew.setNativePlace("社区");
                customerNew.setNativePlace("社区");
                customerNew.setProvince("130000");
                customerNew.setRiskLevel("0");
                customerNew.setSex("1");
                customerNew.setRole(v);
                customerNew.setTandBRelationship("01");
                customerNew.setIdentifyType("01");
                customerNew.setIdentifyNumber("14020319951205702X");
                customerNew.setMobile("15100000000");
                customerNew.setName("张三");
            } else {
                customerNew.setAddr("社区");
                customerNew.setAnnualSalary("M01");
                customerNew.setBirthday("1990-07-08 09:00:00");
                customerNew.setCervalidDate("2023-06-12 00:00:00");
                customerNew.setCity("130100");
                customerNew.setCreendDate("2043-06-12 00:00:00");
                customerNew.setDetailedAddressInfo("河北省石家庄市长安区社区");
                customerNew.setDistrict("130102");
                customerNew.setEmail("555@163.com");
                customerNew.setType("02");
                customerNew.setIdentifyValidDate("2023-06-12 02:46:24");
                customerNew.setIdentifyValidEndDate("2043-06-12 12:00:00");
                customerNew.setNation("1");
                customerNew.setJob("P0001");
                customerNew.setNationality("156");
                customerNew.setNativePlace("社区");
                customerNew.setNativePlace("社区");
                customerNew.setProvince("130000");
                customerNew.setRiskLevel("0");
                customerNew.setSex("1");
                customerNew.setRole(v);
                customerNew.setIdentifyType("01");
                customerNew.setIdentifyNumber("14020319951205702X");
                customerNew.setMobile("15100000000");
                customerNew.setName("张三");

            }
            customer.add(customerNew);
        });


        businessData.setCustomer(customer);


        InsuredObject insuredObject = new InsuredObject();

        PolicyCar policyCar = new PolicyCar();
        policyCar.setEngine("565713688");
        policyCar.setIsNewEnergyFlag("false");
        policyCar.setIssueDate("2023-06-18 00:00:00");
        policyCar.setModel("江淮HFC7150BNF");
        policyCar.setMotorTypeCode("A0");
        policyCar.setMotorUsageTypeCode("8A");
        policyCar.setNewCarSign(true);
        policyCar.setPlateNo("冀");
        policyCar.setRegisterDate("2023-06-18 00:00:00");
        policyCar.setTransferMark(false);
        policyCar.setTransferMarkBi(false);
        policyCar.setVehicleType("K33");
        policyCar.setVin("63712567956271111");

        SimpleStadarCar simpleStadarCar = new SimpleStadarCar();
        simpleStadarCar.setApprovedLoad(0);
        simpleStadarCar.setApprovedPassengersCapacity(5);
        simpleStadarCar.setBrand("江淮");
        simpleStadarCar.setBrandCode("JHA0");
        simpleStadarCar.setCarAlias("和悦1.5L MT出租版");
        simpleStadarCar.setCarYear("");
        simpleStadarCar.setDisplacement(1.499);
        simpleStadarCar.setEnergyTypes("0");
        simpleStadarCar.setGrossMass(0);
        simpleStadarCar.setLocalModelCode("JHAFQD0099");
        simpleStadarCar.setModelCode("BJHAHAUA0002");
        simpleStadarCar.setModelName("江淮HFC7150BNF两用燃料轿车");
        simpleStadarCar.setPower(83);
        simpleStadarCar.setPurchasePrice(63200);
        simpleStadarCar.setPurchasePriceTax(68602);
        simpleStadarCar.setSeries("和悦");
        simpleStadarCar.setSeriesCode("JHA0AE");
        simpleStadarCar.setTractionMass(0);
        simpleStadarCar.setUnladenMass(1385);

        insuredObject.setPolicyCar(policyCar);
        insuredObject.setSimpleStadarCar(simpleStadarCar);

        businessData.setInsuredObject(insuredObject);


        String genericReqString = JSONObject.toJSONString(businessData);

        String signString = Tools.encryptByPublicKey(genericReqString);


//        String genericReqString = "{\"businessMain\":{\"saleSys\":\"53\"},\"businessType\":\"T\",\"contract\":[{\"contractMain\":{\"expiryDate\":\"2024-07-28 23:59:59\",\"proposalType\":\"1\",\"riskCode\":\"0507\",\"validDate\":\"2023-10-29 00:00:00\"},\"coverage\":[{\"kindCode\":\"BZ\",\"kindName\":\"机动车交通事故责任强制保险\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1}],\"tax\":{\"taxConditionCode\":\"1N\",\"taxPayerIdentificationCode\":\"14020319951205702X\",\"taxPayerIdentity\":\"01\",\"taxPayerIdentityNo\":\"14020319951205702X\",\"taxPayerIdentityType\":\"01\",\"taxPayerName\":\"张三\"}},{\"contractMain\":{\"expiryDate\":\"2024-07-28 23:59:59\",\"proposalType\":\"1\",\"riskCode\":\"0510\",\"validDate\":\"2023-10-29 00:00:00\"},\"coverage\":[{\"amount\":137529.6,\"kindCode\":\"A\",\"kindName\":\"车损险\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1},{\"amount\":3000000,\"kindCode\":\"B\",\"kindName\":\"机动车第三者责任险\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1},{\"amount\":10000,\"kindCode\":\"D3\",\"kindName\":\"机动车车上人员责任险(驾驶员)\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1},{\"amount\":40000,\"kindCode\":\"D4\",\"kindName\":\"机动车车上人员责任险(乘客)\",\"quantity\":4,\"relatedInd\":\"0\",\"servicetimes\":1,\"unitAmount\":10000},{\"amount\":5000,\"kindCode\":\"L\",\"kindName\":\"车身划痕损失险\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1},{\"kindCode\":\"Z2\",\"kindName\":\"车辆安全检测\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1},{\"kindCode\":\"Z3\",\"kindName\":\"代为驾驶服务\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1}],\"platFormMessageBean\":{}}],\"contractGlass\":[{\"contractMain\":{\"validDate\":\"2023-09-29 00:00:00\",\"expiryDate\":\"2024-07-28 23:59:59\",\"proposalType\":\"1\",\"eachDamageLimitAmount\":\"0\",\"paragraph\":\"1\",\"insuredObjectCode\":\"0\",\"planSubCode\":\"005\",\"discount\":100,\"nvehRiskName\":\"安享车膜\",\"nvehRiskCode\":\"0311\",\"choosed\":true,\"planCode\":\"CX0311030\",\"nvehRiskType\":\"01\",\"unitPremium\":0,\"allQuantity\":\"1\",\"certNo\":\"\",\"operationNumber\":\"\",\"fuelName\":\"\",\"businessSite\":\"\",\"insureArea\":\"\"},\"coverage\":[{\"unitAmount\":10000,\"dutyCode\":\"066000\",\"dutyName\":\"前挡风玻璃贴膜\",\"term\":\"一年\",\"unitPremium\":0,\"eachDamageLimitAmount\":0,\"seatCount\":\"1\",\"rate\":0.1,\"kindCode\":\"Z03012-1\",\"kindName\":\"车窗玻璃贴膜损失保险条款\"}]}],\"nvehInfo\":[{\"choosed\":true,\"nvehRiskCode\":\"1727\",\"nvehRiskName\":\"道路危险货运承运人责任保险\",\"nvehRiskType\":\"01\",\"planInfo\":[{\"choosed\":true,\"commissionRate\":0,\"expandRate\":0,\"listInsureArea\":[],\"paragraph\":\"1\",\"performanceRate\":0,\"planCode\":\"CX1727001\",\"planName\":\"测试0322\",\"certNo\":\"45556665566\",\"operationNumber\":\"456435645654\",\"unitPremium\":0,\"planSubInfo\":[{\"allQuantity\":\"1\",\"choosed\":true,\"discount\":100,\"eachDamageLimitAmount\":\"0\",\"eachDamageLimitAmountUpperList\":[{\"code\":\"100\",\"value\":\"100\"}],\"expiryDate\":\"\",\"insuredObjectCode\":\"2\",\"insuredObjectName\":\"乘客\",\"isLimitFlag\":\"0\",\"limAmount\":[{\"code\":\"1\",\"value\":\"1\"},{\"code\":\"2\",\"value\":\"2\"},{\"code\":\"3\",\"value\":\"3\"},{\"code\":\"4\",\"value\":\"4\"},{\"code\":\"5\",\"value\":\"5\"}],\"listDiscount\":[{\"code\":\"100\",\"value\":\"100\"}],\"planNameSub\":\"道危乘客\",\"planSubCode\":\"005\",\"riskInfo\":[{\"allAmount\":\"0\",\"clauseUrl\":\"http://byd-product.chinahuanong.com.cn/product-factory/file/viewNew?id=4673ba1e-53c9-4463-bb74-c10aa86692bb&amp;clauseCode=Z17020&amp;type=pdf\",\"deductible\":0,\"dutyCode\":\"0270\",\"dutyName\":\"第三者责任-人身伤亡及财产损失\",\"eachDamageLimitAmount\":0,\"kindCode\":\"Z17020\",\"kindName\":\"道路危险货物承运人责任保险条款(2021版)\",\"listUnitAmount\":[{\"code\":\"10000000\",\"value\":\"10000000\"}],\"rate\":0.01,\"remark\":\" \",\"seatCount\":\"2\",\"term\":\"一年\",\"unitAmount\":\"10000000\",\"unitAmountAdd\":\"1000.0万元/座\",\"unitPremium\":0}],\"seatCountLower\":\"\",\"seatCountUpper\":\"\",\"toncountLower\":\"\",\"toncountUpper\":\"\",\"validDate\":\"2023-10-04\"}]}]}],\"homeInfo\":[{\"choosed\":true,\"nvehRiskCode\":\"2785\",\"nvehRiskName\":\"疫路无忧\",\"nvehRiskType\":\"01\",\"planInfo\":[{\"choosed\":true,\"commissionRate\":0,\"expandRate\":0,\"listInsureArea\":[],\"paragraph\":\"1\",\"performanceRate\":0,\"planCode\":\"CX2785069\",\"planName\":\"测试条款替换\",\"certNo\":\"\",\"operationNumber\":\"\",\"unitPremium\":0,\"planSubInfo\":[{\"allQuantity\":\"1\",\"choosed\":true,\"discount\":100,\"eachDamageLimitAmount\":\"0\",\"eachDamageLimitAmountUpperList\":[{\"code\":\"100\",\"value\":\"100\"}],\"expiryDate\":\"\",\"insuredObjectCode\":\"1\",\"insuredObjectName\":\"驾驶人\",\"isLimitFlag\":\"0\",\"limAmount\":[{\"code\":\"1\",\"value\":\"1\"},{\"code\":\"2\",\"value\":\"2\"},{\"code\":\"3\",\"value\":\"3\"},{\"code\":\"4\",\"value\":\"4\"},{\"code\":\"5\",\"value\":\"5\"}],\"listDiscount\":[{\"code\":\"100\",\"value\":\"100\"}],\"planNameSub\":\"测试条款替换\",\"planSubCode\":\"001\",\"riskInfo\":[{\"allAmount\":\"0\",\"clauseUrl\":\"http://byd-product.chinahuanong.com.cn/product-factory/file/viewNew?id=3dd0e8bc-678e-4074-a3d0-701aa9e4dbde&amp;clauseCode=Z27017&amp;type=pdf\",\"deductible\":0,\"dutyCode\":\"0010\",\"dutyName\":\"意外身故保险责任\",\"eachDamageLimitAmount\":0,\"kindCode\":\"Z27017\",\"kindName\":\"意外身故保险责任\",\"listUnitAmount\":[{\"code\":\"10000\",\"value\":\"10000\"}],\"rate\":0.1,\"remark\":\" \",\"seatCount\":\"1\",\"term\":\"一年\",\"unitAmount\":\"10000\",\"unitAmountAdd\":\"1.0万元/座\",\"unitPremium\":0}],\"seatCountLower\":\"0\",\"seatCountUpper\":\"9999\",\"toncountLower\":\"0\",\"toncountUpper\":\"9999\",\"validDate\":\"2023-10-04\"}]}]}],\"glassInfo\":[{\"choosed\":true,\"nvehRiskCode\":\"0311\",\"nvehRiskName\":\"安享车膜\",\"nvehRiskType\":\"01\",\"planInfo\":[{\"choosed\":true,\"commissionRate\":0,\"expandRate\":0,\"listInsureArea\":[],\"paragraph\":\"1\",\"performanceRate\":0,\"planCode\":\"CX0311030\",\"planName\":\"0519测试\",\"certNo\":\"\",\"operationNumber\":\"\",\"unitPremium\":0,\"planSubInfo\":[{\"allQuantity\":\"1\",\"choosed\":true,\"discount\":100,\"eachDamageLimitAmount\":\"0\",\"eachDamageLimitAmountUpperList\":[{\"code\":\"100\",\"value\":\"100\"}],\"expiryDate\":\"\",\"insuredObjectCode\":\"1\",\"insuredObjectName\":\"驾驶人\",\"isLimitFlag\":\"0\",\"limAmount\":[{\"code\":\"1\",\"value\":\"1\"},{\"code\":\"2\",\"value\":\"2\"},{\"code\":\"3\",\"value\":\"3\"},{\"code\":\"4\",\"value\":\"4\"},{\"code\":\"5\",\"value\":\"5\"}],\"listDiscount\":[{\"code\":\"100\",\"value\":\"100\"}],\"planNameSub\":\"测试\",\"planSubCode\":\"005\",\"riskInfo\":[{\"allAmount\":\"0\",\"clauseUrl\":\"http://product.chinahuanong.com.cn/product-factory/file/viewNew?id=8d81279d-8650-479f-953b-16883d67eb93&amp;clauseCode=Z03012&amp;type=docx\",\"deductible\":0,\"dutyCode\":\"066000\",\"dutyName\":\"前挡风玻璃贴膜\",\"eachDamageLimitAmount\":0,\"kindCode\":\"Z03012-1\",\"kindName\":\"车窗玻璃贴膜损失保险条款\",\"listUnitAmount\":[{\"code\":\"10000\",\"value\":\"10000\"}],\"rate\":0.1,\"remark\":\" \",\"seatCount\":\"1\",\"term\":\"一年\",\"unitAmount\":\"10000\",\"unitAmountAdd\":\"1.0万元/座\",\"unitPremium\":0}],\"seatCountLower\":\"0\",\"seatCountUpper\":\"9999\",\"toncountLower\":\"0\",\"toncountUpper\":\"9999\",\"validDate\":\"2023-10-04\"}]}]}],\"customer\":[{\"addr\":\"社区\",\"annualSalary\":\"M01\",\"birthday\":\"1990-07-08 09:00:00\",\"cervalidDate\":\"2023-06-12 00:00:00\",\"city\":\"130100\",\"creendDate\":\"2043-06-12 00:00:00\",\"detailedAddressInfo\":\"河北省石家庄市长安区社区\",\"district\":\"130102\",\"email\":\"555@163.com\",\"identifyNumber\":\"14020319951205702X\",\"identifyType\":\"01\",\"identifyValidDate\":\"2023-06-12 02:46:24\",\"identifyValidEndDate\":\"2043-06-12 12:00:00\",\"job\":\"P0001\",\"mobile\":\"15100000000\",\"name\":\"张三\",\"nation\":\"1\",\"nationality\":\"156\",\"nativePlace\":\"社区\",\"province\":\"130000\",\"riskLevel\":\"0\",\"role\":\"1\",\"sex\":\"1\",\"tandBRelationship\":\"01\",\"type\":\"02\"},{\"addr\":\"社区\",\"annualSalary\":\"M01\",\"birthday\":\"1990-07-08 09:00:00\",\"cervalidDate\":\"2023-06-12 00:00:00\",\"city\":\"130100\",\"creendDate\":\"2043-06-12 00:00:00\",\"detailedAddressInfo\":\"河北省石家庄市长安区社区\",\"district\":\"130102\",\"email\":\"555@163.com\",\"identifyNumber\":\"14020319951205702X\",\"identifyType\":\"01\",\"identifyValidDate\":\"2023-06-12 02:46:24\",\"identifyValidEndDate\":\"2043-06-12 12:00:00\",\"job\":\"P0001\",\"mobile\":\"15100000000\",\"name\":\"张三\",\"nation\":\"1\",\"nationality\":\"156\",\"nativePlace\":\"社区\",\"province\":\"130000\",\"riskLevel\":\"0\",\"role\":\"2\",\"sex\":\"1\",\"type\":\"02\"},{\"addr\":\"社区\",\"annualSalary\":\"M01\",\"birthday\":\"1990-07-08 09:00:00\",\"cervalidDate\":\"2023-06-12 00:00:00\",\"city\":\"130100\",\"creendDate\":\"2043-06-12 00:00:00\",\"detailedAddressInfo\":\"河北省石家庄市长安区社区\",\"district\":\"130102\",\"email\":\"555@163.com\",\"identifyNumber\":\"14020319951205702X\",\"identifyType\":\"01\",\"identifyValidDate\":\"2023-06-12 02:46:24\",\"identifyValidEndDate\":\"2043-06-12 12:00:00\",\"job\":\"P0001\",\"mobile\":\"15100000000\",\"name\":\"张三\",\"nation\":\"1\",\"nationality\":\"156\",\"nativePlace\":\"社区\",\"province\":\"130000\",\"riskLevel\":\"0\",\"role\":\"3\",\"sex\":\"1\",\"type\":\"02\"}],\"head\":{\"interfaceCode\":\"ZTYQ\",\"token\":\""+token+"\",\"transCode\":\"QuotedPrice\",\"transType\":\"Req\"},\"insuredObject\":{\"policyCar\":{\"engine\":\"565713688\",\"isNewEnergyFlag\":\"false\",\"issueDate\":\"2023-06-18 00:00:00\",\"model\":\"江淮HFC7150BNF\",\"motorTypeCode\":\"A0\",\"motorUsageTypeCode\":\"8A\",\"newCarSign\":true,\"plateNo\":\"冀\",\"registerDate\":\"2023-06-18 00:00:00\",\"transferMark\":false,\"transferMarkBi\":false,\"vehicleType\":\"K33\",\"vin\":\"63712567956271111\"},\"simpleStadarCar\":{\"approvedLoad\":0,\"approvedPassengersCapacity\":5,\"brand\":\"江淮\",\"brandCode\":\"JHA0\",\"carAlias\":\"和悦1.5L MT出租版\",\"carYear\":\"\",\"displacement\":1.499,\"energyTypes\":\"0\",\"grossMass\":0,\"localModelCode\":\"JHAFQD0099\",\"modelCode\":\"BJHAHAUA0002\",\"modelName\":\"江淮HFC7150BNF两用燃料轿车\",\"power\":83,\"purchasePrice\":63200,\"purchasePriceTax\":68602,\"series\":\"和悦\",\"seriesCode\":\"JHA0AE\",\"tractionMass\":0,\"unladenMass\":1385}}}";
//        String signString = Tools.encryptByPublicKey(genericReqString);


        //String genericReqShortString001 ="{\"businessMain\":{\"saleSys\":\"53\"},\"businessType\":\"T\",\"contract\":[{\"contractMain\":{\"expiryDate\":\"2024-07-28 23:59:59\",\"proposalType\":\"1\",\"riskCode\":\"0507\",\"validDate\":\"2023-10-29 00:00:00\"},\"coverage\":[{\"kindCode\":\"BZ\",\"kindName\":\"机动车交通事故责任强制保险\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1}],\"tax\":{\"taxConditionCode\":\"1N\",\"taxPayerIdentificationCode\":\"14020319951205702X\",\"taxPayerIdentity\":\"01\",\"taxPayerIdentityNo\":\"14020319951205702X\",\"taxPayerIdentityType\":\"01\",\"taxPayerName\":\"张三\"}},{\"contractMain\":{\"expiryDate\":\"2024-07-28 23:59:59\",\"proposalType\":\"1\",\"riskCode\":\"0510\",\"validDate\":\"2023-10-29 00:00:00\"},\"coverage\":[{\"amount\":137529.6,\"kindCode\":\"A\",\"kindName\":\"车损险\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1},{\"amount\":3000000,\"kindCode\":\"B\",\"kindName\":\"机动车第三者责任险\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1}],\"platFormMessageBean\":{}}],\"customer\":[{\"addr\":\"社区\",\"annualSalary\":\"M01\",\"birthday\":\"1990-07-08 09:00:00\",\"cervalidDate\":\"2023-06-12 00:00:00\",\"city\":\"130100\",\"creendDate\":\"2043-06-12 00:00:00\",\"detailedAddressInfo\":\"河北省石家庄市长安区社区\",\"district\":\"130102\",\"email\":\"555@163.com\",\"identifyNumber\":\"14020319951205702X\",\"identifyType\":\"01\",\"identifyValidDate\":\"2023-06-12 02:46:24\",\"identifyValidEndDate\":\"2043-06-12 12:00:00\",\"job\":\"P0001\",\"mobile\":\"15100000000\",\"name\":\"张三\",\"nation\":\"1\",\"nationality\":\"156\",\"nativePlace\":\"社区\",\"province\":\"130000\",\"riskLevel\":\"0\",\"role\":\"1\",\"sex\":\"1\",\"tandBRelationship\":\"01\",\"type\":\"02\"}],\"head\":{\"interfaceCode\":\"ZTYQ\",\"token\":\""+token+"\",\"transCode\":\"QuotedPrice\",\"transType\":\"Req\"},\"insuredObject\":{\"policyCar\":{\"engine\":\"565713688\",\"isNewEnergyFlag\":\"false\",\"issueDate\":\"2023-06-18 00:00:00\",\"model\":\"江淮HFC7150BNF\",\"motorTypeCode\":\"A0\",\"motorUsageTypeCode\":\"8A\",\"newCarSign\":true,\"plateNo\":\"冀\",\"registerDate\":\"2023-06-18 00:00:00\",\"transferMark\":false,\"transferMarkBi\":false,\"vehicleType\":\"K33\",\"vin\":\"63712567956271111\"},\"simpleStadarCar\":{\"approvedLoad\":0,\"approvedPassengersCapacity\":5,\"brand\":\"江淮\",\"brandCode\":\"JHA0\",\"carAlias\":\"和悦1.5L MT出租版\",\"carYear\":\"\",\"displacement\":1.499,\"energyTypes\":\"0\",\"grossMass\":0,\"localModelCode\":\"JHAFQD0099\",\"modelCode\":\"BJHAHAUA0002\",\"modelName\":\"江淮HFC7150BNF两用燃料轿车\",\"power\":83,\"purchasePrice\":63200,\"purchasePriceTax\":68602,\"series\":\"和悦\",\"seriesCode\":\"JHA0AE\",\"tractionMass\":0,\"unladenMass\":1385}}}";


        //String genericReqShortString002 = "{\"businessMain\":{\"saleSys\":\"53\"},\"businessType\":\"T\",\"contract\":[{\"contractMain\":{\"expiryDate\":\"2024-07-28 23:59:59\",\"proposalType\":\"1\",\"riskCode\":\"0507\",\"validDate\":\"2023-10-29 00:00:00\"},\"coverage\":[{\"kindCode\":\"BZ\",\"kindName\":\"机动车交通事故责任强制保险\",\"quantity\":1,\"relatedInd\":\"0\",\"servicetimes\":1}],\"tax\":{\"taxConditionCode\":\"1N\",\"taxPayerIdentificationCode\":\"14020319951205702X\",\"taxPayerIdentity\":\"01\",\"taxPayerIdentityNo\":\"14020319951205702X\",\"taxPayerIdentityType\":\"01\",\"taxPayerName\":\"张三\"}}],\"contractGlass\":[{\"contractMain\":{\"validDate\":\"2023-09-29 00:00:00\",\"expiryDate\":\"2024-07-28 23:59:59\",\"proposalType\":\"1\",\"eachDamageLimitAmount\":\"0\",\"paragraph\":\"1\",\"insuredObjectCode\":\"0\",\"planSubCode\":\"005\",\"discount\":100,\"nvehRiskName\":\"安享车膜\",\"nvehRiskCode\":\"0311\",\"choosed\":true,\"planCode\":\"CX0311030\",\"nvehRiskType\":\"01\",\"unitPremium\":0,\"allQuantity\":\"1\",\"certNo\":\"\",\"operationNumber\":\"\",\"fuelName\":\"\",\"businessSite\":\"\",\"insureArea\":\"\"},\"coverage\":[{\"unitAmount\":10000,\"dutyCode\":\"066000\",\"dutyName\":\"前挡风玻璃贴膜\",\"term\":\"一年\",\"unitPremium\":0,\"eachDamageLimitAmount\":0,\"seatCount\":\"1\",\"rate\":0.1,\"kindCode\":\"Z03012-1\",\"kindName\":\"车窗玻璃贴膜损失保险条款\"}]}],\"customer\":[{\"addr\":\"社区\",\"annualSalary\":\"M01\",\"birthday\":\"1990-07-08 09:00:00\",\"cervalidDate\":\"2023-06-12 00:00:00\",\"city\":\"130100\",\"creendDate\":\"2043-06-12 00:00:00\",\"detailedAddressInfo\":\"河北省石家庄市长安区社区\",\"district\":\"130102\",\"email\":\"555@163.com\",\"identifyNumber\":\"14020319951205702X\",\"identifyType\":\"01\",\"identifyValidDate\":\"2023-06-12 02:46:24\",\"identifyValidEndDate\":\"2043-06-12 12:00:00\",\"job\":\"P0001\",\"mobile\":\"15100000000\",\"name\":\"张三\",\"nation\":\"1\",\"nationality\":\"156\",\"nativePlace\":\"社区\",\"province\":\"130000\",\"riskLevel\":\"0\",\"role\":\"1\",\"sex\":\"1\",\"tandBRelationship\":\"01\",\"type\":\"02\"},{\"addr\":\"社区\",\"annualSalary\":\"M01\",\"birthday\":\"1990-07-08 09:00:00\",\"cervalidDate\":\"2023-06-12 00:00:00\",\"city\":\"130100\",\"creendDate\":\"2043-06-12 00:00:00\",\"detailedAddressInfo\":\"河北省石家庄市长安区社区\",\"district\":\"130102\",\"email\":\"555@163.com\",\"identifyNumber\":\"14020319951205702X\",\"identifyType\":\"01\",\"identifyValidDate\":\"2023-06-12 02:46:24\",\"identifyValidEndDate\":\"2043-06-12 12:00:00\",\"job\":\"P0001\",\"mobile\":\"15100000000\",\"name\":\"张三\",\"nation\":\"1\",\"nationality\":\"156\",\"nativePlace\":\"社区\",\"province\":\"130000\",\"riskLevel\":\"0\",\"role\":\"2\",\"sex\":\"1\",\"type\":\"02\"},{\"addr\":\"社区\",\"annualSalary\":\"M01\",\"birthday\":\"1990-07-08 09:00:00\",\"cervalidDate\":\"2023-06-12 00:00:00\",\"city\":\"130100\",\"creendDate\":\"2043-06-12 00:00:00\",\"detailedAddressInfo\":\"河北省石家庄市长安区社区\",\"district\":\"130102\",\"email\":\"555@163.com\",\"identifyNumber\":\"14020319951205702X\",\"identifyType\":\"01\",\"identifyValidDate\":\"2023-06-12 02:46:24\",\"identifyValidEndDate\":\"2043-06-12 12:00:00\",\"job\":\"P0001\",\"mobile\":\"15100000000\",\"name\":\"张三\",\"nation\":\"1\",\"nationality\":\"156\",\"nativePlace\":\"社区\",\"province\":\"130000\",\"riskLevel\":\"0\",\"role\":\"3\",\"sex\":\"1\",\"type\":\"02\"}],\"head\":{\"interfaceCode\":\"ZTYQ\",\"token\":\"" + token + "\",\"transCode\":\"QuotedPrice\",\"transType\":\"Req\",\"sign\":\"\"},\"insuredObject\":{\"policyCar\":{\"engine\":\"565713688\",\"isNewEnergyFlag\":\"false\",\"issueDate\":\"2023-06-18 00:00:00\",\"model\":\"江淮HFC7150BNF\",\"motorTypeCode\":\"A0\",\"motorUsageTypeCode\":\"8A\",\"newCarSign\":true,\"plateNo\":\"冀\",\"registerDate\":\"2023-06-18 00:00:00\",\"transferMark\":false,\"transferMarkBi\":false,\"vehicleType\":\"K33\",\"vin\":\"63712567956271111\"},\"simpleStadarCar\":{\"approvedLoad\":0,\"approvedPassengersCapacity\":5,\"brand\":\"江淮\",\"brandCode\":\"JHA0\",\"carAlias\":\"和悦1.5L MT出租版\",\"carYear\":\"\",\"displacement\":1.499,\"energyTypes\":\"0\",\"grossMass\":0,\"localModelCode\":\"JHAFQD0099\",\"modelCode\":\"BJHAHAUA0002\",\"modelName\":\"江淮HFC7150BNF两用燃料轿车\",\"power\":83,\"purchasePrice\":63200,\"purchasePriceTax\":68602,\"series\":\"和悦\",\"seriesCode\":\"JHA0AE\",\"tractionMass\":0,\"unladenMass\":1385}}}";

        //String signShortString = Tools.encryptByPublicKey(genericReqShortString002);

        return huaNongBizClient.quotedPrice(JSONObject.parseObject(signString));
    }

    //{"orderNo":"8a80938d8ab6af16018ab6b406f31b80","vin":"63712567956271111"}
    @PostMapping(value = "/submit")
    @ResponseBody
    public String submit(@RequestHeader(value = "token") String token,
                         @RequestHeader(value = "transCode") String transCode,
                         @RequestBody Map<String, String> requestMap) {

        BuildRequestHead buildRequestHead = new BuildRequestHead(token, transCode).invoke();
        GenericReq genericReq = buildRequestHead.getGenericReq();

        DeliveryInfo deliveryInfo = new DeliveryInfo();
        deliveryInfo.setCity("石家庄市");
        deliveryInfo.setDetail("社区");
        deliveryInfo.setDistrict("长安区");
        deliveryInfo.setMobile("15100000000");
        deliveryInfo.setName("张三");
        deliveryInfo.setProvince("河北省");

        genericReq.setAttribute("head", buildRequestHead.getHeaderMap());
        genericReq.setAttribute("deliveryInfo", deliveryInfo);
        genericReq.setAttribute("vin", requestMap.get("vin"));
        genericReq.setAttribute("undWrtNoticeUrl", "http://cars.huahai16888.com:8088/platform//hn/underwritingStatus/callback");
        genericReq.setAttribute("callbackUrl", "http://cars.huahai16888.com:8088/platform/hn/underwriting/callback");
        genericReq.setAttribute("orderNo", requestMap.get("orderNo"));
        genericReq.setAttribute("appliType", requestMap.get("appliType"));
        String signString = Tools.encryptByPublicKey(JSONObject.toJSONString(genericReq));
        return huaNongBizClient.submit(JSONObject.parseObject(signString));
    }

    //{"orderNo":["8a80938d8ab6af16018ab6b406f31b80"]
    @PostMapping(value = "/applyForPay")
    @ResponseBody
    public String applyForPay(@RequestHeader(value = "token") String token,
                              @RequestHeader(value = "transCode") String transCode,
                              @RequestBody Map<String, String> requestMap) throws IOException {
        BuildRequestHead buildRequestHead = new BuildRequestHead(token, transCode).invoke();
        GenericReq genericReq = buildRequestHead.getGenericReq();
        Map<String, String> headerMap = buildRequestHead.getHeaderMap();
        genericReq.setAttribute("head", headerMap);
        genericReq.setAttribute("orderNo", Arrays.asList(requestMap.get("orderNo")));
        String signString = Tools.encryptByPublicKey(JSONObject.toJSONString(genericReq));
        return huaNongBizClient.applyForPay(JSONObject.parseObject(signString));
    }

    //{"orderNo":"8a80938d8ab6af16018ab6b406f31b80"}
    @ResponseBody
    @PostMapping(value = "/orderQuery")
    public String orderQuery(@RequestHeader(value = "token") String token,
                             @RequestHeader(value = "transCode") String transCode,
                             @RequestBody String orderNo) {
        BuildRequestHead buildRequestHead = new BuildRequestHead(token, transCode).invoke();
        GenericReq genericReq = buildRequestHead.getGenericReq();
        genericReq.setAttribute("head", buildRequestHead.getHeaderMap());
        genericReq.setAttribute("OrderNo", orderNo);
        String signString = Tools.encryptByPublicKey(JSONObject.toJSONString(genericReq));
        return huaNongBizClient.orderQuery(JSONObject.parseObject(signString));
    }

    @ResponseBody
    @PostMapping(value = "/customcollect")
    public String customcollect(@RequestHeader(value = "token") String token,
                                @RequestHeader(value = "transCode") String transCode) {
        BuildRequestHead buildRequestHead = new BuildRequestHead(token, transCode).invoke();
        GenericReq genericReq = buildRequestHead.getGenericReq();
        genericReq.setAttribute("head", buildRequestHead.getHeaderMap());
        genericReq.setAttribute("proposalType", "0");
        genericReq.setAttribute("saleSys", "53");
        List<Customer> customer = new ArrayList<Customer>();
        String[] roles = {"1", "2", "3"};
        Arrays.stream(roles).forEach(v -> {
            Customer customerNew = new Customer();
            if ("1".equals(v)) {
                customerNew.setAddr("社区");
                customerNew.setAnnualSalary("M01");
                customerNew.setBirthday("1990-07-08 09:00:00");
                customerNew.setCervalidDate("2023-06-12 00:00:00");
                customerNew.setCity("130100");
                customerNew.setCreendDate("2043-06-12 00:00:00");
                customerNew.setDetailedAddressInfo("河北省石家庄市长安区社区");
                customerNew.setDistrict("130102");
                customerNew.setEmail("555@163.com");
                customerNew.setType("02");
                customerNew.setIdentifyIssuedCom("大同市公安局");
                customerNew.setIdentifyValidDate("2023-06-12 02:46:24");
                customerNew.setIdentifyValidEndDate("2043-06-12 12:00:00");
                customerNew.setNation("1");
                customerNew.setJob("P0001");
                customerNew.setNationality("156");
                customerNew.setNativePlace("社区");
                customerNew.setNativePlace("社区");
                customerNew.setProvince("130000");
                customerNew.setRiskLevel("0");
                customerNew.setSex("1");
                customerNew.setRole(v);
                customerNew.setTandBRelationship("01");
                customerNew.setIdentifyType("01");
                customerNew.setIdentifyNumber("14020319951205702X");
                customerNew.setMobile("15100000000");
                customerNew.setName("张三");
            } else {
                customerNew.setAddr("社区");
                customerNew.setAnnualSalary("M01");
                customerNew.setBirthday("1990-07-08 09:00:00");
                customerNew.setCervalidDate("2023-06-12 00:00:00");
                customerNew.setCity("130100");
                customerNew.setCreendDate("2043-06-12 00:00:00");
                customerNew.setDetailedAddressInfo("河北省石家庄市长安区社区");
                customerNew.setDistrict("130102");
                customerNew.setEmail("555@163.com");
                customerNew.setType("02");
                customerNew.setIdentifyIssuedCom("大同市公安局");
                customerNew.setIdentifyValidDate("2023-06-12 02:46:24");
                customerNew.setIdentifyValidEndDate("2043-06-12 12:00:00");
                customerNew.setNation("1");
                customerNew.setJob("P0001");
                customerNew.setNationality("156");
                customerNew.setNativePlace("社区");
                customerNew.setNativePlace("社区");
                customerNew.setProvince("130000");
                customerNew.setRiskLevel("0");
                customerNew.setSex("1");
                customerNew.setRole(v);
                customerNew.setIdentifyType("01");
                customerNew.setIdentifyNumber("14020319951205702X");
                customerNew.setMobile("15100000000");
                customerNew.setName("张三");

            }
            customer.add(customerNew);
        });
        genericReq.setAttribute("customers", customer);

        String signString = Tools.encryptByPublicKey(JSONObject.toJSONString(genericReq));
        return huaNongBizClient.orderQuery(JSONObject.parseObject(signString));
    }



    @ResponseBody
    @PostMapping(value = "/verifyCode")
    public String verifyCode(@RequestHeader(value = "token") String token,
                             @RequestHeader(value = "transCode") String transCode,
                             @RequestBody Map<String,String> paramsMap) {
        BuildRequestHead buildRequestHead = new BuildRequestHead(token, transCode).invoke();
        GenericReq genericReq = buildRequestHead.getGenericReq();
        genericReq.setAttribute("head", buildRequestHead.getHeaderMap());
        genericReq.setAttribute("orderNo", paramsMap.get("orderNo"));
        genericReq.setAttribute("email", "705366435@qq.com");
        genericReq.setAttribute("mobile", "13393939898");
        String signString = Tools.encryptByPublicKey(JSONObject.toJSONString(genericReq));

        return huaNongBizClient.verifyCode(JSONObject.parseObject(signString));
    }



    private class BuildRequestHead {
        private String token;
        private String transCode;
        private GenericReq genericReq;
        private Map<String, String> headerMap;

        public BuildRequestHead(String token, String transCode) {
            this.token = token;
            this.transCode = transCode;
        }

        public GenericReq getGenericReq() {
            return genericReq;
        }

        public Map<String, String> getHeaderMap() {
            return headerMap;
        }

        public BuildRequestHead invoke() {
            genericReq = new GenericReq();
            headerMap = new HashMap<String, String>(16);
            headerMap.put("interfaceCode", "ZTYQ");
            headerMap.put("token", token);
            headerMap.put("transCode", transCode);
            headerMap.put("transType", "Req");
            return this;
        }
    }
}
